package uk.co.hephistocles.blobGame;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.CatmullRomSpline;
import com.badlogic.gdx.math.Vector3;

public class CatmullRomPath extends CatmullRomSpline implements Path {
	@Override
	public Vector3 getPos(int segment, float t) {
		Vector3 P0 = getControlPoints().get(segment);
		Vector3 P1 = getControlPoints().get(segment+1);
		Vector3 P2 = getControlPoints().get(segment+2);
		Vector3 P3 = getControlPoints().get(segment+3);
		int px = (int) (0.5*(P0.x*(t*((2-t)*t -1)) + 
				P1.x*(t*t*(3*t -5) + 2) + 
				P2.x*(t*((4-3*t)*t + 1))+
				P3.x*((t-1)*t*t)));
		int py = (int) (0.5*(P0.y*(t*((2-t)*t -1)) + 
				P1.y*(t*t*(3*t -5) + 2) + 
				P2.y*(t*((4-3*t)*t + 1))+
				P3.y*((t-1)*t*t)));
		int pz = (int) (0.5*(P0.z*(t*((2-t)*t -1)) + 
				P1.z*(t*t*(3*t -5) + 2) + 
				P2.z*(t*((4-3*t)*t + 1))+
				P3.z*((t-1)*t*t)));
		
		return new Vector3(px,py,pz);
	}

	@Override
	public int getSegmentsNum() {
		return getControlPoints().size()-3;
	}

	@Override
	public Vector3 getPosFromDist(int seg, float s) {
		//TODO: Implement getPosFromDist by scaling from total length of curve
		return null;
	}

	@Override
	public void init(FileHandle file) {
		String[] catmullRom = file.readString().split("\r");
		int count = catmullRom.length;
		//loop through the file to get CatmullRom points
		for (int i=0; i<count; i++) {
			this.add(new Vector3((int)Float.parseFloat( catmullRom[i].split(" ")[0]),
					(int) Float.parseFloat(catmullRom[i].split(" ")[1]), 0));
		}
	}
}
